package com.atguigu.common;

import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import springfox.documentation.annotations.ApiIgnore;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author panghl
 * @Date 2020/12/25 16:41
 * @Description 统一返回对象
 **/
@Data
@ApiModel(description = "返回相应数据")
public class R {

    @ApiModelProperty(value = "是否成功")
    private Boolean success;

    @ApiModelProperty(value = "返回码")
    private Integer code;

    @ApiModelProperty(value = "返回消息")
    private String msg;

    @ApiModelProperty(value = "返回数据")
    private Object data ;

    @JsonIgnore
    private  static Map<String,Object> map = new HashMap<>();

    //把构造方法私有
    private R() {}

    //成功静态方法
    public static R ok() {
    R r = new R();
    r.setSuccess(true);
    r.setCode(ResultCode.SUCCESS.getCode());
    r.setMsg("成功");
    return r;
  }

    //失败静态方法
    public static R error() {
    R r = new R();
    r.setSuccess(false);
    r.setCode(ResultCode.ERROR.getCode());
    r.setMsg("失败");
    return r;
  }

    public R success(Boolean success){
    this.setSuccess(success);
    return this;
  }

    public R message(String message){
    this.setMsg(message);
    return this;
  }

    public R code(Integer code){
    this.setCode(code);
    return this;
  }

    public R data(String key, Object value){
    map.put(key,value);
    this.setData(map);
    return this;
  }

  //返回this 是为了链式编程
    public R data(Object data){
    this.setData(data);
    return this;
  }


}
